package sp.servlet;

import com.alibaba.fastjson.JSON;
import sp.service.NovelService;
import sp.service.NovelServiceImpl;
import sp.dao.Page;
import sp.entity.Novel;
import sp.util.ParamUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/novel")
public class NovelServlet extends HttpServlet {
    NovelService novelService = new NovelServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        String classId = req.getParameter("classId");
        String name = req.getParameter("name");
        String author = req.getParameter("author");
        int page = ParamUtil.parseInt(req.getParameter("page"),1);
        int rows = ParamUtil.parseInt(req.getParameter("rows"),2);
        Page<Novel> pageBean = novelService.findPage(classId,name,author,page,rows);
        List<Novel> novelsList = novelService.findAll();
        Map map = new HashMap();
        map.put("data",pageBean);
        map.put("NovelIdList",novelsList);
        resp.getWriter().write(JSON.toJSONString(map));
        resp.getWriter().flush();
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        String id = req.getParameter("id");
        String resultFormat = "{\"state\":\"%b\",\"error\":\"%s\"}";
        String result = "";
        try {
            novelService.delete(id);
            result = String.format(resultFormat,true,"");
        } catch (RuntimeException e){
            result  = String.format(resultFormat,false,e.getMessage());
        }
        resp.getWriter().write(result);
        resp.flushBuffer();
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
//        String classId = req.getParameter("classId");
        Novel novel = new Novel(req.getParameter("id"),"",
                                req.getParameter("name"),
                                req.getParameter("author"),
                                req.getParameter("description"),
                                req.getParameter("chapterCount"));
        String resultFormat = "{\"state\":\"%b\",\"error\":\"%s\"}";
        String result = "";

        try {
            novelService.updateNovel(novel);
            result = String.format(resultFormat,true,"");
        } catch (RuntimeException e){
            result  = String.format(resultFormat,false,e.getMessage());
       }
        resp.getWriter().write(result);
        resp.flushBuffer();
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        Novel novel = new Novel(req.getParameter("id"),
                req.getParameter("classId"),
                req.getParameter("name"),
                req.getParameter("author"),
                req.getParameter("desc"),
                req.getParameter("chap"));
        String resultFormat = "{\"state\":\"%b\",\"error\":\"%s\"}";
        String result = "";
        try {
            novelService.addNovel(novel);
            result = String.format(resultFormat,true,"");
        } catch (RuntimeException e){
                result  = String.format(resultFormat,false,e.getMessage());
        }
        resp.getWriter().write(result);
        resp.flushBuffer();
    }
}
